package com.github.glideplus.impl;


import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.IdRes;

import com.bumptech.glide.RequestManager;


/**
 * Created by zoudong on 2017/2/19.
 */

public  class DisplayOptions {

    private int placeholderResourceId;
    private int errorResourceId;
    protected Drawable placeholderDrawable;
    protected Drawable errorDrawable;
    protected RoundModel roundModel;
    protected boolean isCircle = false;
    protected boolean isCenterCrop = false;

    public DisplayOptions(int placeholderResourceId, int errorResourceId, Drawable placeholderDrawable, Drawable errorDrawable, RoundModel roundModel, boolean isCircle, boolean isCenterCrop) {
        this.placeholderResourceId = placeholderResourceId;
        this.errorResourceId = errorResourceId;
        this.placeholderDrawable = placeholderDrawable;
        this.errorDrawable = errorDrawable;
        this.roundModel = roundModel;
        this.isCircle = isCircle;
        this.isCenterCrop = isCenterCrop;
    }


    public DisplayOptions() {
    }

    public Drawable getErrorDrawable(Context context) {
        if (errorDrawable == null && errorResourceId > 0) {
            errorDrawable = context.getResources().getDrawable(errorResourceId);
        }
        return errorDrawable;
    }

    public Drawable getPlaceholderDrawable(Context context) {
        if (placeholderDrawable == null && placeholderResourceId > 0) {
            placeholderDrawable = context.getResources().getDrawable(placeholderResourceId);
        }
        return placeholderDrawable;
    }

    public int getPlaceholderResourceId() {
        return placeholderResourceId;
    }

    public int getErrorResourceId() {
        return errorResourceId;
    }

    public DisplayOptions builder() {
        return this;
    }

    public static class RoundModel {
        public enum CornerType {
            ALL,
            TOP_LEFT, TOP_RIGHT, BOTTOM_LEFT, BOTTOM_RIGHT,
            TOP, BOTTOM, LEFT, RIGHT,
            OTHER_TOP_LEFT, OTHER_TOP_RIGHT, OTHER_BOTTOM_LEFT, OTHER_BOTTOM_RIGHT,
            DIAGONAL_FROM_TOP_LEFT, DIAGONAL_FROM_TOP_RIGHT
        }

        private CornerType cornerType = CornerType.ALL;
        private int mRounded_Radius;
        private int mRounded_Margin;

        public RoundModel(int mRounded_Radius, int mRounded_Margin) {
            this.mRounded_Radius = mRounded_Radius;
            this.mRounded_Margin = mRounded_Margin;
        }

        public RoundModel(CornerType cornerType, int rounded_Radius, int rounded_Margin) {
            this.cornerType = cornerType;
            this.mRounded_Radius = rounded_Radius;
            this.mRounded_Margin = rounded_Margin;
        }

        public CornerType getCornerType() {
            return cornerType;
        }

        public int getRounded_Radius() {
            return mRounded_Radius;
        }

        public int getRounded_Margin() {
            return mRounded_Margin;
        }
    }
    public  static Builder defaultOptions(){
        return  new Builder().centerCrop();
    }
    public  static class Builder{
        private int placeholderResourceId;
        private int errorResourceId;
        private Drawable placeholderDrawable;
        private Drawable errorDrawable;
        private RoundModel roundModel;
        private boolean isCircle = false;
        private boolean isCenterCrop = false;

        public Builder() {
        }

        public Builder placeholder(@IdRes int res) {
            this.placeholderResourceId = res;
            return this;
        }

        public Builder errorholder(@IdRes int res) {
            this.errorResourceId = res;
            return this;
        }

        public Builder placeholder(Drawable drawable) {
            this.placeholderDrawable = drawable;
            return this;
        }

        public Builder errorholder(Drawable drawable) {
            this.errorDrawable = drawable;
            return this;
        }

        public Builder cropCircle() {
            this.isCircle = true;
            return this;
        }

        public Builder centerCrop() {
            this.isCenterCrop = true;
            return this;
        }

        public Builder roundCorners(RoundModel model) {
            this.roundModel = model;
            return this;
        }
        public DisplayOptions build(){
            return new DisplayOptions(placeholderResourceId,errorResourceId,placeholderDrawable,errorDrawable,roundModel,isCircle,isCenterCrop);
        }
    }
}
